
package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWage;
import cn.stylefeng.guns.sys.modular.system.service.BtyWageService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 *
 * @author fengshuonan
 * @Date 2017-05-09 23:02:21
 */
@Controller
@RequestMapping("/btyWage")
public class BtyWageController extends BaseController {

    private String PREFIX = "/modular/system/btyWage/";
    private String userName="admin";
    /**
     *是否是督导的标志off
     */
    private String off="off";
    @Autowired
    private BtyWageService btyWageService;
    /**
     * 跳转到通知列表首页
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:06 PM
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyWage.html";
    }
    /**
     * 获取通知列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:06 PM
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String dm,String status) {
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getAccount();
        String username=LoginContextHolder.getContext().getUser().getName();
        //获取是否是督导
        String rem=LoginContextHolder.getContext().getUser().getAvatar();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        //获取当前月的上个月
        String lastMonth= DateUtil.getLastMonth();
        Page<Map<String, Object>> list;
        //noinspection AlibabaUndefineMagicConstant
        String roleName="人事";
        String flag="";
        if(roleNames.contains(roleName)){
            list = this.btyWageService.list(condition,dm,null,lastMonth,status,null,null,flag);
            Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
            return LayuiPageFactory.createPageInfo(wrap);
        }
        if(!(userName.equals(account))){
            flag="8";
            if(rem.contains(off)){
                list = this.btyWageService.list(condition,dm,null,lastMonth,status,username,null,flag);
                if(list.getRecords().size()==0){
                    list = this.btyWageService.list(condition,dm,account,lastMonth,status,null,null,flag);
                }
            }else{
                list = this.btyWageService.list(condition,dm,null,lastMonth,status,null,username,flag);
                if(list.getRecords().size()==0){
                    list = this.btyWageService.list(condition,dm,account,lastMonth,status,null,username,flag);
                }

            }
        }else{

            list = this.btyWageService.list(condition,dm,null,lastMonth,status,null,null,flag);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }
    /**
     * 批量审核确认
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmOk")
    @ResponseBody
    public Object confirmOk(@RequestParam String ids) {
        String[] idss = ids.split(",");
        for(String id:idss){
            BtyWage btyWage=btyWageService.getById(id);
            String bz=btyWage.getBz()==null?"":btyWage.getBz();
            String remark=bz+"9";
            btyWage.setBz(remark);
            btyWageService.updateById(btyWage);
        }
        return SUCCESS_TIP;
    }
    /**
     * 批量取消审核
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmQx")
    @ResponseBody
    public Object confirmQx(@RequestParam String ids) {
        String[] idss = ids.split(",");
        for(String id:idss){
            BtyWage btyWage=btyWageService.getById(id);
            String bz=btyWage.getBz()==null?"":btyWage.getBz();
            String remark="";
            if(bz.contains("9")){
                bz=bz.substring(0,bz.indexOf("9"));
                remark=bz;
            }
            btyWage.setBz(remark);
            btyWageService.updateById(btyWage);
        }
        return SUCCESS_TIP;
    }
    /**
     * 查询店名
     *
     * @author liuy
     *
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyArchives> getDictionaryType(){
        //获取是否是督导
        String rem=LoginContextHolder.getContext().getUser().getAvatar();
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getName();
        String phone=LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyArchives> list = null;
        if(roleNames.contains("人事")){
            account="admin";
            list = this.btyWageService.dmList(account,null,null);
            return list;
        }
        if(!(userName.equals(account))){
            if(rem.contains(off)){
                 list = this.btyWageService.dmList(account,null,null);
                if(list.size()==0){
                    list = this.btyWageService.dmList(null,null,phone);
                }
            }else{
                list = this.btyWageService.dmList(null,account,null);
                if(list.size()==0){
                    list = this.btyWageService.dmList(null,account,phone);
                }
            }
        }else{
            list = this.btyWageService.dmList(account,null,null);
        }

        return list;
    }
}
